package com.baosight.sczx.zm.ct.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.ct.DataInterface;

public class ServiceZMCT09 extends ServiceEPBase 
{
	public EiInfo showQuery(EiInfo inInfo)
	{
		List<String> column_list = new ArrayList<String>();
		EiBlock block = new EiBlock(EiConstant.resultBlock);
		
		String fdSql = inInfo.get("fdSql").toString();
		String fdDs = inInfo.get("fdDs").toString();
		String fdColumns = fdSql.substring(0, fdSql.toLowerCase().indexOf("from"));
		fdColumns = fdColumns.substring(fdColumns.indexOf(" ")+1, fdColumns.trim().length());
		String[] columns = fdColumns.split(",");
		
		for(int i=0; i<columns.length; i++)
		{
			String column = columns[i];
			column = column.replaceAll("\t", " ");
			column = column.replaceAll("\r", " ");
			column = column.replaceAll("\n", " ");
			String fdFieldTname = "";
			if(column.toLowerCase().contains(" as "))
			{
				fdFieldTname = column.trim().substring(column.trim().lastIndexOf(" "), column.trim().length()).trim().replaceAll("\"", "");
			}
			
			if(column.trim().contains(" "))
			{
				fdFieldTname = column.trim().substring(column.trim().lastIndexOf(" "), column.trim().length()).trim().replaceAll("\"", "");
			}
			else
			{
				fdFieldTname = column.trim();
			}
			
			column_list.add(fdFieldTname);
			EiColumn eiColumn = new EiColumn(fdFieldTname);
			eiColumn.setDescName(fdFieldTname);
			eiColumn.setType("C");
			block.addMeta(eiColumn);
		}

		if(fdSql.toLowerCase().contains(":currentdate"))
		{
			fdSql = fdSql.toLowerCase().replaceAll(":currentdate", StaticTools.getDate());
		}
		String value = query(column_list, block, fdDs, fdSql);
		if(!"1".equals(value))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg(value);
		}
		else
		{
			inInfo.set("fdSql", fdSql);
			inInfo.addBlock(block);
		}
		
		return inInfo;
	}
	
	private String query(List<String> column_list, EiBlock block, String fdDs, String fdSql)
	{
		DataInterface dataInterface = new DataInterface(fdDs);
		if(!dataInterface.drive())
		{
			return "找不到驱动程序类 ，加载驱动失败";
		}
		if(!dataInterface.connect())
		{
			return "数据库连接失败";
		}
		
		ResultSet rs = dataInterface.search(fdSql);
		int i = 0;
		if(rs != null)
		{
			 try
			 {
				 while(rs.next())
				 {
					 if(i>100)
					 {
						 break;
					 }
					 for(int j=0; j<column_list.size(); j++)
					 {
						 String fdColumnName = column_list.get(j);
						 block.setCell(i, fdColumnName, rs.getString(fdColumnName));
					 }
					 
					 i++;
				 }
				 
				 if(i == 0)
				 {
					 for(int j=0; j<column_list.size(); j++)
					 {
						 String fdColumnName = column_list.get(j);
						 block.setCell(0, fdColumnName, "");
					 }
				 }
			 }
			 catch(SQLException e)
			 {
				return "请检查sql语句的正确性，确保在选中的数据库配置中，可正常运行";
			 } 
		}
		else
		{
			return "请检查sql语句的正确性，确保在选中的数据库配置中，可正常运行";
		}
		
		return "1";
	}
}
